package com.ruoyi.swla2.controller;

import cn.hutool.core.util.CoordinateUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.swla2.domain.RealtimeGatherData;
import com.ruoyi.swla2.domain.Zone;
import com.ruoyi.swla2.domain.ZoneJoin;
import com.ruoyi.swla2.model.ZoneJoinModel;
import com.ruoyi.swla2.service.IRealtimeGatherDataService;
import com.ruoyi.swla2.service.IZoneJoinService;
import com.ruoyi.swla2.service.IZoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 分区关联Controller
 *
 * @author gdqt
 * @date 2023-06-16
 */
@RestController
@RequestMapping("/region/zoneJoin")
public class ZoneJoinController extends BaseController {
    @Autowired
    private IZoneJoinService zoneJoinService;

    @Autowired
    private IRealtimeGatherDataService realtimeGatherDataService;

    /**
     * 查询分区关联列表
     */
    @PreAuthorize("@ss.hasPermi('region:zone:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZoneJoin zoneJoin) {
        startPage();
        List<ZoneJoin> list = zoneJoinService.selectZoneJoinList(zoneJoin);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('region:zone:list')")
    @GetMapping("/getZoneJoinTerminals")
    public TableDataInfo getZoneJoinTerminals(ZoneJoinModel model) {
        List<ZoneJoinModel> list = zoneJoinService.getZoneJoinTerminals(model);
        List<RealtimeGatherData> realtimeGatherDataList = realtimeGatherDataService.selectRealtimegatherdataList(new RealtimeGatherData());
        list.forEach(zjm -> {
            RealtimeGatherData realtimeGatherData = realtimeGatherDataList.stream().filter(item -> item.getTerminalID().equals(zjm.getTerminalId())).findFirst().orElse(null);
            zjm.setRealtimeGatherData(realtimeGatherData);
            // 使用定位控件定位的经纬度不需要转换坐标
            if (StrUtil.isNotEmpty(zjm.getLat()) && StrUtil.isNotEmpty(zjm.getLat()) && zjm.getLngLatTemp() == null) {
                String lng = zjm.getLng();
                String lat = zjm.getLat();
                CoordinateUtil.Coordinate coordinate = CoordinateUtil.bd09ToGcj02(Double.parseDouble(lng), Double.parseDouble(lat));
                zjm.setLng(String.valueOf(coordinate.getLng()));
                zjm.setLat(String.valueOf(coordinate.getLat()));
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出分区关联列表
     */
    @PreAuthorize("@ss.hasPermi('region:zone:export')")
    @Log(title = "分区关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZoneJoin zoneJoin) {
        List<ZoneJoin> list = zoneJoinService.selectZoneJoinList(zoneJoin);
        ExcelUtil<ZoneJoin> util = new ExcelUtil<ZoneJoin>(ZoneJoin.class);
        util.exportExcel(response, list, "分区关联数据");
    }

    /**
     * 获取分区关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('region:zone:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(zoneJoinService.selectZoneJoinByID(ID));
    }

    /**
     * 新增分区关联
     */
    @PreAuthorize("@ss.hasPermi('region:zone:add')")
    @Log(title = "分区关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZoneJoin zoneJoin) {
        zoneJoin.setTenantId(this.getTenantId());
        return toAjax(zoneJoinService.insertZoneJoin(zoneJoin));
    }

    /**
     * 修改分区关联
     */
    @PreAuthorize("@ss.hasPermi('region:zone:edit')")
    @Log(title = "分区关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZoneJoin zoneJoin) {
        return toAjax(zoneJoinService.updateZoneJoin(zoneJoin));
    }

    /**
     * 删除分区关联
     */
    @PreAuthorize("@ss.hasPermi('region:zone:remove')")
    @Log(title = "分区关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(zoneJoinService.deleteZoneJoinByIDs(IDs));
    }
}
